package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.SelectBean;
import com.wnhz.work.yz.view.NoScrollExListview;

import java.util.List;

public class LookDActivity extends BaseActivity implements View.OnClickListener {

    private NoScrollExListview expanded;
    private ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_d);
        TextView title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("套餐详情");
        findViewById(R.id.btn_main_left).setOnClickListener(this);

        img = (ImageView) findViewById(R.id.img);

        expanded = (NoScrollExListview) findViewById(R.id.expanded);
        setData2((SelectBean.InfoEntity) getIntent().getSerializableExtra("info1"));
    }

    private SelectBean.InfoEntity info1 = new SelectBean.InfoEntity();

    private void setData2(SelectBean.InfoEntity info1) {
        this.info1 = info1;

        if (info1 == null) {
            MyToast("暂无数据");
            return;
        }
        ImageLoader.getInstance().displayImage(info1.getImg(), img);
        TextView content = (TextView) findViewById(R.id.content);
        content.setText(info1.getDetail());
        TextView price = (TextView) findViewById(R.id.price);
        price.setText("¥" + info1.getPrice());
        findViewById(R.id.goto2).setOnClickListener(this);
        final List<SelectBean.InfoEntity.ListEntity> list = info1.getList();


        expanded.setAdapter(new BaseExpandableListAdapter() {
            @Override
            public int getGroupCount() {
                return list.size();
            }

            @Override
            public int getChildrenCount(int groupPosition) {
                return list.get(groupPosition).getCate().size();
            }

            @Override
            public Object getGroup(int groupPosition) {
                return list.get(groupPosition);
            }

            @Override
            public Object getChild(int groupPosition, int childPosition) {
                return list.get(groupPosition).getCate().get(childPosition);
            }

            @Override
            public long getGroupId(int groupPosition) {
                return groupPosition;
            }

            @Override
            public long getChildId(int groupPosition, int childPosition) {
                return childPosition;
            }

            @Override
            public boolean hasStableIds() {
                return false;
            }

            @Override
            public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
                convertView = View.inflate(LookDActivity.this, R.layout.selegr, null);
                TextView textView = (TextView) convertView.findViewById(R.id.title);
                textView.setText(list.get(groupPosition).getClassname());
                ImageView icon = (ImageView) convertView.findViewById(R.id.icon);
                if (expanded.isGroupExpanded(groupPosition)) {
                    icon.setImageResource(R.mipmap.ic_rightarrow);
                } else {
                    icon.setImageResource(R.mipmap.nav_bottom_iocn);
                }
                return convertView;
            }

            @Override
            public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {


                convertView = View.inflate(LookDActivity.this, R.layout.selegr2, null);
                TextView textView = (TextView) convertView.findViewById(R.id.title);
                textView.setText(list.get(groupPosition).getCate().get(childPosition).getName());
                ImageView head = (ImageView) convertView.findViewById(R.id.head);
                ImageLoader.getInstance().displayImage(list.get(groupPosition).getCate().get(childPosition).getImg(), head);

                TextView price = (TextView) convertView.findViewById(R.id.price);
                price.setText("¥ " + list.get(groupPosition).getCate().get(childPosition).getPrice());
                TextView num = (TextView) convertView.findViewById(R.id.num);
                num.setText("x" + list.get(groupPosition).getCate().get(childPosition).getNums());
                return convertView;
            }

            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                return groupPosition == childPosition;
            }
        });
        // expanded.expandGroup(0);
        for (int i = 0; i < list.size(); i++) {
            expanded.expandGroup(i);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;
            case R.id.goto2:
                Intent intent = new Intent(this, InstallYuYueActivity.class);
                intent.putExtra("selectId", info1.getFid());
                intent.putExtra("your_name", "");
                intent.putExtra("your_address", "");
                intent.putExtra("your_phone", "");
                intent.putExtra("your_area", "");
                intent.putExtra("type", "0");
                intent.putExtra("pid", getIntent().getStringExtra("pid"));
                intent.putExtra("price", "" + info1.getPrice());
                startActivity(intent);
                break;
        }
    }
}
